package chapter01;

public class Divide {
    //溢出 kuo'bei
    public int divide(int dividend, int divisor) {
        if(dividend==Integer.MIN_VALUE&&divisor==-1){
            return Integer.MAX_VALUE;
        }
        int flag=((dividend>0)^(divisor>0))?-1:1;
        int res=0;
        if(dividend>0){
            dividend=-dividend;
        }
        if(divisor>0){
            divisor=-divisor;
        }
        while (dividend<=divisor){
            int k=1;
            int value=divisor;
            while (value>=0xc0000000&&dividend<value+value){
                k+=k;
                value+=value;
            }
            dividend-=value;
            res+=k;
        }

        return flag==1?res:-res;
    }

    public static void main(String[] args) {
        System.out.println(new Divide().divide(-10,3));
    }
}
